﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SsTool.Metadata.Windows;
using SmartSpace.Data;

namespace SsTool.Metadata
{
    /// <summary>
    /// Interaction logic for DictionaryPage.xaml
    /// </summary>
    public partial class DictionaryPage : Page
    {
        public DictionaryPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(DictionaryPage_Loaded);
        }

        void DictionaryPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadTypes();
        }

        private void LoadTypes()
        {
            IDataService service = App.Get<IDataService>();
            _ctlListType.ItemsSource = service.FindDictionaryTypes();
        }

        private void LoadDatas(DictionaryType type)
        {
            IDataService service = App.Get<IDataService>();
            this._ctlTreeData.ItemsSource = service.FindDictionaryDatas(type);
        }

        private void _ctlBtnCreateType_Click(object sender, RoutedEventArgs e)
        {
            DictionaryTypeWindow window = new DictionaryTypeWindow();
            if (window.ShowDialog() == true)
            {
                LoadTypes();
            }
        }

        private void _ctlBtnCreateData_Click(object sender, RoutedEventArgs e)
        {
            DictionaryData selectedData = _ctlTreeData.SelectedItem as DictionaryData;
            DictionaryType selectedType = _ctlListType.SelectedItem as DictionaryType;

            if (selectedType != null)
            {
                if (selectedData != null)
                {
                    DictionaryDataWindow window = new DictionaryDataWindow(selectedType, selectedData);
                    if (window.ShowDialog() == true)
                    {
                        this.LoadDatas(selectedType);
                    }
                }
                else
                {
                    DictionaryDataWindow window = new DictionaryDataWindow(selectedType);
                    if (window.ShowDialog() == true)
                    {
                        this.LoadDatas(selectedType);
                    }
                }
            }
        }

        private void _ctlListType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                DictionaryType type = e.AddedItems[0] as DictionaryType;
                if (type != null)
                {
                    LoadDatas(type);
                }
            }
        }
    }
}
